###############################
###############################
###Figure 1.1: Conflict Maps###
###############################
###############################
#Load relevant libraries
library(foreign)
library(doBy)
library(dplyr)
library(gganimate)
library(ggplot2)
library(maps)
library(ggthemes)
library(doBy)

#############################
#############################
###Arranging the Map Data###
#############################
#############################

##set working directory
setwd("~/OneDrive - Indiana University/FromGoogle/SouthSudanProject/Code_12_20_24/")
#read conflict data
acled.geo <- read.csv("acledgeo92024.csv")
summary(acled.geo)


###Create Juba coordinates manually
coordinate_juba <- data.frame(t(c(1,4.85165,31.58247)))
names(coordinate_juba) <- c("city", "lat","long")
coordinate_juba$city <- ifelse(coordinate_juba$city==1, "Juba",coordinate_juba$city)
summary(coordinate_juba)

################################
################################
###Plot map for all countries###
################################
################################

########################
###CW Actor Conflicts###
########################
acled.geo$year<-acled.geo$YEAR
out.dat2 <- acled.geo[,c("LATITUDE","LONGITUDE","acled_all_cw", "month", "year")]
#Collapse by lat and long
out.dat.col2 <- summaryBy(acled_all_cw~LATITUDE+LONGITUDE, data=out.dat2, FUN=c("sum"),keep.names = T, na.rm=T)
out.dat.col.nz2 <- subset(out.dat.col2,acled_all_cw>0 )
table(out.dat.col.nz2$acled_all_cw)

##sort
out.dat.col.nz2 <- out.dat.col.nz2[with(out.dat.col.nz2, order(acled_all_cw)), ]

#Create a discrete conflict cateogry
out.dat.col.nz2$classe <- ifelse(out.dat.col.nz2$acled_all_cw <= 10,"1-10",
                                 ifelse((out.dat.col.nz2$acled_all_cw > 10 & out.dat.col.nz2$acled_all_cw <= 20), "10-20",
                                        ifelse((out.dat.col.nz2$acled_all_cw > 20 & out.dat.col.nz2$acled_all_cw <= 30), "20-30",
                                               ifelse((out.dat.col.nz2$acled_all_cw > 30 & out.dat.col.nz2$acled_all_cw <= 40), "30-40",
                                                      ifelse((out.dat.col.nz2$acled_all_cw > 40 & out.dat.col.nz2$acled_all_cw <= 50), "40-50",
                                                             ifelse((out.dat.col.nz2$acled_all_cw > 50 & out.dat.col.nz2$acled_all_cw <= 75), "50-75",
                                                                    ifelse((out.dat.col.nz2$acled_all_cw > 75 & out.dat.col.nz2$acled_all_cw <= 99), "75-99",
                                                                           "More than 99")))))))

unique(out.dat.col.nz2$classe)

#Color scale
cc <- scales::seq_gradient_pal("pink", "darkred", "Lab")(seq(0,1,length.out=length(unique(out.dat.col.nz2$classe))))

##Define countries to be plotted
african.countries <- c("South Sudan")
#Begin with a map of the African states
world <- ggplot() +
  borders("world", regions=african.countries, colour = "gray85", fill = "gray80") +
  theme_map() 

#Plot
map.all.col2 <- world +
  geom_point(aes(x = LONGITUDE, y = LATITUDE, color = classe),
             data = out.dat.col.nz2, alpha=0.8,  size=1.5) +
  scale_colour_manual(name="Civil war events", values=cc)+
  theme(legend.position="bottom")+
  geom_text(data = coordinate_juba, aes(label = city,x = long, y = lat), size=4, vjust = -1,fontface = "bold")

  #North star
 # ggspatial::annotation_north_arrow(
 #   location = "ttr", #which_north = "true",
  #  pad_x = unit(0.4, "in"), pad_y = unit(0.4, "in"),
  #  style = ggspatial::north_arrow_nautical(
  #    fill = c("grey40", "white"),
   #   line_col = "grey20"
   # )
  #)+
  #geom_text(aes(x = 48.5, y = -50.5, 
       #         label = "Scale 1:100,000,000"),size=3,fontface='bold')

##Export map
jpeg("Figure11a.jpeg", width = 6, height = 6, units = 'in', res = 500)
map.all.col2
dev.off()


############################
###Social Actor Conflicts###
############################
out.dat3 <- acled.geo[,c("LATITUDE","LONGITUDE","acled_all_sc", "month", "year")]
#Collapse by lat and long
out.dat.col3 <- summaryBy(acled_all_sc~LATITUDE+LONGITUDE, data=out.dat3, FUN=c("sum"),keep.names = T, na.rm=T)
out.dat.col.nz3 <- subset(out.dat.col3,acled_all_sc>0 )
table(out.dat.col.nz3$acled_all_sc)


##sort
out.dat.col.nz3 <- out.dat.col.nz3[with(out.dat.col.nz3, order(acled_all_sc)), ]

#Create a discrete conflict cateogry
out.dat.col.nz3$classe <- ifelse(out.dat.col.nz3$acled_all_sc <= 10,"1-10",
                                 ifelse((out.dat.col.nz3$acled_all_sc > 10 & out.dat.col.nz3$acled_all_sc <= 20), "10-20",
                                        ifelse((out.dat.col.nz3$acled_all_sc > 20 & out.dat.col.nz3$acled_all_sc <= 30), "20-30",
                                               ifelse((out.dat.col.nz3$acled_all_sc > 30 & out.dat.col.nz3$acled_all_sc <= 40), "30-40",
                                                      ifelse((out.dat.col.nz3$acled_all_sc > 40 & out.dat.col.nz3$acled_all_sc <= 50), "40-50",
                                                             ifelse((out.dat.col.nz3$acled_all_sc > 50 & out.dat.col.nz3$acled_all_sc <= 75), "50-75",
                                                                    ifelse((out.dat.col.nz3$acled_all_sc > 75 & out.dat.col.nz3$acled_all_sc <= 99), "75-99",
                                                                           "More than 99")))))))

unique(out.dat.col.nz3$classe)

#Color scale
cc <- scales::seq_gradient_pal("pink", "darkred", "Lab")(seq(0,1,length.out=length(unique(out.dat.col.nz3$classe))))

##Define countries to be plotted
african.countries <- c("South Sudan")
#Begin with a map of the African states
world <- ggplot() +
  borders("world", regions=african.countries, colour = "gray85", fill = "gray80") +
  theme_map() 

#Plot
map.all.col3 <- world +
  geom_point(aes(x = LONGITUDE, y = LATITUDE, color = classe),
             data = out.dat.col.nz3, alpha=0.8, size=1.5) +
  scale_colour_manual(name="Social conflict events", values=cc)+
  theme(legend.position="bottom")+
  geom_text(data = coordinate_juba, aes(label = city,x = long, y = lat), size=4, vjust = -1,fontface = "bold")

#North star
# ggspatial::annotation_north_arrow(
#   location = "ttr", #which_north = "true",
#  pad_x = unit(0.4, "in"), pad_y = unit(0.4, "in"),
#  style = ggspatial::north_arrow_nautical(
#    fill = c("grey40", "white"),
#   line_col = "grey20"
# )
#)+
#geom_text(aes(x = 48.5, y = -50.5, 
#         label = "Scale 1:100,000,000"),size=3,fontface='bold')

##Export map
jpeg("Figure11b.jpeg", width = 6, height = 6, units = 'in', res = 500)
map.all.col3
dev.off()


